package com.bfly.mall.product.api;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.ActionModel;
import com.bfly.core.security.Login;
import com.bfly.mall.product.entity.Product;
import com.bfly.mall.product.dto.ProductListDTO;
import com.bfly.mall.product.service.IProductFavoriteService;
import com.bfly.mall.product.service.IProductService;
import com.bfly.mall.user.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品收藏API
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 19:42
 */
@RestController
@RequestMapping(value = "/api/product")
public class ProductFavoriteApiController extends BaseController {

    @Autowired
    private IProductFavoriteService favoriteService;
    @Autowired
    private IProductService productService;

    /**
     * 判断是否收藏过该产品
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/12 19:49
     */
    @GetMapping(value = "/check/favorite")
    public void checkFavorite(HttpServletResponse response, @RequestParam("productId") int productId) {
        User user = getUser();
        boolean isFavorite = user == null ? false : favoriteService.isFavorite(user.getId(), productId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(isFavorite));
    }

    /**
     * 收藏产品
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/12 19:49
     */
    @GetMapping(value = "/favorite")
    @Login
    @ActionModel("收藏产品")
    public void favorite(HttpServletResponse response, @RequestParam("productId") int productId) {
        User user = getUser();
        favoriteService.save(user.getId(), productId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 取消收藏产品
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/12 19:49
     */
    @GetMapping(value = "/list/favorite")
    @Login
    public void listFavorite(HttpServletResponse response) {
        User user = getUser();
        List<Product> productFavorite = productService.getProductFavorite(user.getId());
        List<ProductListDTO> list = new ArrayList<>();
        if (productFavorite != null) {
            for (Product product : productFavorite) {
                ProductListDTO dto = new ProductListDTO();
                BeanUtils.copyProperties(product, dto);

                dto.setCategoryName(product.getCategory().getName());
                dto.setSales(product.getVirtualSales());
                list.add(dto);
            }
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(list));
    }

    /**
     * 收藏列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/1 12:45
     */
    @GetMapping(value = "/unfavorite")
    @Login
    @ActionModel("取消收藏产品")
    public void unFavorite(HttpServletResponse response, @RequestParam("productId") int productId) {
        User user = getUser();
        favoriteService.unFavorite(user.getId(), productId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}